'Name:			oeTime.vb														##
'Author:		schachmat														##
'Last modified:	5.9.2007														##
'Status:																		##
'	functionality:			0%													##
'	documentation intern:	0%													##
'	documentation extern:	0%													##
'Comments:																		##
'TODO: ComparExtend																##
'																				##
'																				##
'																				##
'																				##
'#################################################################################


'The MIT License

'Copyright (c) 2007 Markus Teich

'Permission is hereby granted, free of charge, to any person obtaining a copy
'of this software and associated documentation files (the "Software"), to deal
'in the Software without restriction, including without limitation the rights
'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the Software is
'furnished to do so, subject to the following conditions:

'The above copyright notice and this permission notice shall be included in
'all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
'THE SOFTWARE.

Namespace Core
    Namespace Time

        Public MustInherit Class oeTimeSource
            Public MustOverride ReadOnly Property Time() As Double
        End Class

        Public Class oeTimeSourceWin32HiRes
            Inherits oeTimeSource

            Protected m_HiResFrequency As Int64 = 0

            <System.Runtime.InteropServices.DllImport("Kernel32.dll")> _
            Protected Shared Function QueryPerformanceCounter(ByRef perfcount As Int64) As Boolean
            End Function

            <System.Runtime.InteropServices.DllImport("Kernel32.dll")> _
            Protected Shared Function QueryPerformanceFrequency(ByRef freq As Int64) As Boolean
            End Function

            Protected ReadOnly Property HiResValue() As Int64
                Get
                    Dim tickCount As Int64
                    QueryPerformanceCounter(tickCount)
                    Return tickCount
                End Get
            End Property

            Public Sub New()
                oeLog.LogInfo("initialising oeTimeSourceWin32HiRes...")

                Dim test As Int64
                If QueryPerformanceCounter(test) Then
                    QueryPerformanceFrequency(m_HiResFrequency)
                Else
                    oeLog.LogError("High-resolution counter not supported.")
                End If

                oeLog.LogOk("oeTimeSourceWin32HiRes initialising complete")
            End Sub

            Public ReadOnly Property HiResFrequency() As Int64
                Get
                    Return m_HiResFrequency
                End Get
            End Property

            Public Shared ReadOnly Property Supported() As Boolean
                Get
                    Return QueryPerformanceCounter(New Int64)
                End Get
            End Property

            Public Overrides ReadOnly Property Time() As Double
                Get
                    Return HiResValue / m_HiResFrequency
                End Get
            End Property


        End Class



        Public Class oeTime
            Public HiResSupported As Boolean = False
            Protected m_HiResFrequency As Int64 = 0

            <System.Runtime.InteropServices.DllImport("Kernel32.dll")> _
            Private Shared Function QueryPerformanceCounter(ByRef perfcount As Int64) As Boolean
            End Function

            <System.Runtime.InteropServices.DllImport("Kernel32.dll")> _
            Private Shared Function QueryPerformanceFrequency(ByRef freq As Int64) As Boolean
            End Function


            Public Sub New()
                oeLog.LogInfo("oeTime wird initialisiert...")

                Dim test As Int64
                If QueryPerformanceCounter(test) Then
                    HiResSupported = True
                    QueryPerformanceFrequency(m_HiResFrequency)
                Else
                    m_HiResFrequency = 1000
                    oeLog.LogWarning("High-resolution counter not supported.")
                End If

                oeLog.LogOk("oeTime erfolgreich initialisiert")
            End Sub

            Public ReadOnly Property HiResFrequency() As Int64
                Get
                    Return m_HiResFrequency
                End Get
            End Property

            Public ReadOnly Property Seconds() As Double
                Get
                    If HiResSupported Then
                        Return HiResValue / m_HiResFrequency
                    Else
                        Return Environment.TickCount
                    End If
                End Get
            End Property

            Protected ReadOnly Property HiResValue() As Int64
                Get
                    Dim tickCount As Int64
                    QueryPerformanceCounter(tickCount)
                    Return tickCount
                End Get
            End Property
        End Class

    End Namespace
End Namespace